package cn.itcast.service.controller;

import cn.itcast.service.client.UserClient;
import cn.itcast.service.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;


@Controller
@RequestMapping("consumer/user")
//@DefaultProperties(defaultFallback = "defaultFallback") // 定义全局的熔断方法
public class UserController {

  /*  private final RestTemplate restTemplate;
//    // 包含了拉取的所有服务
//    private final DiscoveryClient discoveryClient;

    public UserController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
*/

  private final UserClient userClient;

    @Autowired
    public UserController(UserClient userClient) {
        this.userClient = userClient;
    }

    @GetMapping
    @ResponseBody
    @HystrixCommand // 声明熔断的方法
    public String queryUserById(@RequestParam(name = "id") Integer id) {
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
//        ServiceInstance serviceInstance = instances.get(0);
       // return this.restTemplate.getForObject("http://service-provider/user/" + id, String.class);
        return this.userClient.queryUserById(id).toString();
    }
/*
    public String defaultFallback(){
        return "服务器正忙，清稍后再试!";
    }*/
}
